/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.stop.manage.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.close.common.api.feign.RemoteClosedHazardApplyService;
import com.hopes.bizz.close.visitor.vo.ClosedHazardCarApplyVO;
import com.hopes.bizz.stop.common.constant.DriveDicConstant;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopCarRecordDTO;
import com.hopes.bizz.stop.manage.entity.ClosedDriveInCarListEntity;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopCarEntity;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopCarRecordEntity;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopEntity;
import com.hopes.bizz.stop.manage.mapper.ClosedDriveStopCarRecordMapper;
import com.hopes.bizz.stop.manage.service.ClosedDriveInCarListService;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopCarRecordService;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopCarService;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopService;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 园区停车位预约纪录管理
 *
 * @author pig
 * @date 2024-02-19 11:02:38
 */
@Service
@RequiredArgsConstructor
public class ClosedDriveStopCarRecordServiceImpl extends ServiceImpl<ClosedDriveStopCarRecordMapper, ClosedDriveStopCarRecordEntity> implements ClosedDriveStopCarRecordService {

    private final ClosedDriveStopCarService closedDriveStopCarService;
    private final ClosedDriveStopService closedDriveStopService;
    private final ClosedDriveInCarListService closedDriveInCarListService;
    private final RemoteClosedHazardApplyService remoteClosedHazardApplyService;

    @Override
    public R reserve(ClosedDriveStopCarRecordDTO closedDriveStopCarRecordDTO) {
        // 停车类型
        Long carType = closedDriveStopCarRecordDTO.getCarType();
        // 所选停车场
        Long stopParkId = closedDriveStopCarRecordDTO.getStopParkId();
        // 查询该停车场下未预约的停车位
        List<ClosedDriveStopCarEntity> driveStopCarEntities = closedDriveStopCarService.lambdaQuery().eq(ClosedDriveStopCarEntity::getStopParkId, stopParkId)
                .eq(ClosedDriveStopCarEntity::getStopCarType, carType)
                .eq(ClosedDriveStopCarEntity::getUseFlag,Boolean.TRUE)
                .eq(ClosedDriveStopCarEntity::getReserveFlag,Boolean.FALSE)
                .list();
        if (CollUtil.isEmpty(driveStopCarEntities)){
            return  R.failed("该停车场以无此类型的停车位");
        }
        // 随机分配一个并记录
        ClosedDriveStopCarEntity stopCar = driveStopCarEntities.get(RandomUtil.randomInt(driveStopCarEntities.size()));
        stopCar.setReserveFlag(Boolean.TRUE);
        LocalDateTime now = LocalDateTime.now();
        stopCar.setReserveDate(now);
        stopCar.setCarNumber(closedDriveStopCarRecordDTO.getPlateNumber());
        closedDriveStopCarService.updateById(stopCar);
        ClosedDriveStopCarRecordEntity entity = new ClosedDriveStopCarRecordEntity();
        BeanUtils.copyProperties(closedDriveStopCarRecordDTO,entity);
        entity.setStopCarId(stopCar.getId());
        entity.setReserveDate(now);
        save(entity);
        // 修改车位剩余个数
        ClosedDriveStopEntity stopEntity = closedDriveStopService.getById(stopParkId);
        stopEntity.setSurplusPlace(stopEntity.getSurplusPlace() - 1);
        closedDriveStopService.updateById(stopEntity);
        // 增加准入清单记录
        ClosedDriveInCarListEntity carListEntity = new ClosedDriveInCarListEntity();
        BeanUtils.copyProperties(closedDriveStopCarRecordDTO,carListEntity);
        // 预约车位
        carListEntity.setSubCarport(stopEntity.getStopTypeStr()+"["+ stopCar.getStopCarTypeStr()+"]"+stopCar.getStopCarCode());
        carListEntity.setSubCarportId(stopCar.getId());
        carListEntity.setStopStatus(DriveDicConstant.STOP_STATUS_NOT_IN);
        ClosedHazardCarApplyVO closedHazardCarApplyVO = remoteClosedHazardApplyService.queryHazardApplyByBillNo(closedDriveStopCarRecordDTO.getWaybillNumber(), SecurityConstants.FROM_IN).getData();
        closedDriveInCarListService.save(carListEntity);
        return R.ok("预约成功");
    }
}
